var _this ;
layui.use(['form','table','tfApi'], function () {
    var $ = layui.jquery, table = layui.table , form = layui.form, API = layui.tfApi;

    table.render({
        elem: '#addresseeTable',
        url: API.CURL + API.invoice.qryConsigneeList,
        method: "POST",
        contentType: 'application/json',
        xhrFields: {withCredentials: true},crossDomain: true,
        request:{
            pageName: "page",
            limitName : "pageSize"
        },
        parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.details, //解析数据列表
            };
        },
        cols: [[
            {field: 'recivePersonId',title: 'recivePersonId', hide: true},
            {field: 'sellerId',title: 'sellerId', hide: true},
            {field: 'recivePersonName', title: '收件人姓名'},
            {field: 'mobile', title: '收件人手机号'},
            {field: 'address', title: '收件人地址'},
            {title: '操作', toolbar: '#currentTableBar' , align: 'center', minWidth: 150}
        ]],
        page: true,
        height : 'auto'
    });


    table.on('tool(addresseeFilter)', function (obj) {
        var data = obj.data;
        console.log(data);
        if (obj.event === 'edit') {

            var index = layer.open({
                title: '编辑用户',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '../../page/invoice/userInfo.html',
                success: function (layero, index) {
                    var frameId = $(layero).find('iframe').attr("id");
                    //找到子页面的document对象
                    var document = window.frames[frameId].document;
                    //根据id找到子页面中相应的元素并赋值recivePersonId
                    $(document).find("input[name='recivePersonId']").val(data.recivePersonId);
                    $(document).find("input[name='recivePersonName']").val(data.recivePersonName);
                    $(document).find("input[name='mobile']").val(data.mobile);
                    $(document).find("input[name='address']").val(data.address);
                },
                end:function(){
                    location.reload();
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除行么', function (index) {
                var _url = API.invoice.delConsignee,
                    _data={
                        recivePersonId : data.recivePersonId
                    };
                API.postAjax(_url,_data,function (res) {
                    if (res.code == 0){
                        obj.del();
                    }
                })
                layer.close(index);
            });
        }
    });

    //监听提交：添加收件人 确定保存
    form.on('submit(saveBtn)', function (data) {
    	$('#Btn').addClass("layui-btn-disabled").attr("disabled",true);
        var recivePersonId = $("input[name='recivePersonId']").val(),
            recivePersonName = $("input[name='recivePersonName']").val(),
            mobile = $("input[name='mobile']").val(),
            address = $("input[name='address']").val(),
            addFromApply = $("input[name='addFromApply']").val(),
            _data = {
                recivePersonName : recivePersonName,
                mobile : mobile,
                address : address
            },
            _dataEdit = {
                recivePersonId : recivePersonId,
                recivePersonName : recivePersonName,
                mobile : mobile,
                address : address
            };

        if(recivePersonId == ''){ // 添加收件人

            API.postAjax(API.invoice.insConsignee,_data,function (res) {
                if(res.code == 0){
                    var index = layer.alert('添加成功！', {
                        title: '提交信息'
                    }, function () {
                        if(addFromApply=='apply'){
                            // 关闭弹出层：从“发票收件人”进入的“新增”操作
                            window.parent.location.reload();
                        }else{
                            // 关闭弹出层：从“发票收件人”进入的“新增”操作
                            //var _url = '/index.html?#/page/invoice/addressee.html';
                            window.parent.parent.location.reload();
                            //console.log("url ="+ window.parent.location.href);
                            //window.parent.parent.location.href = _url;
                        }
                    });
                }
            });
        }else { //  编辑收件人
            API.postAjax(API.invoice.updConsignee,_dataEdit,function (res) {
                if(res.code == 0){
                    var index = layer.alert('修改成功！', {
                        title: '提交信息'
                    }, function () {
                        // 关闭弹出层
                        var _url = '/index.html?#/page/invoice/addressee.html';
                        window.parent.parent.location.reload();
                        console.log("url ="+ window.parent.location.href);
                        window.parent.parent.location.href = _url;
                        $('#Btn').removeClass("layui-btn-disabled").attr("disabled",false);
                    });
                }
            });
        }

        return false;
    });
    //打开添加收件人弹窗
    $(".ui_box_title_addSjr").click(function () {
        var index = layer.open({
            title: '添加收件人',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: '../../page/invoice/userInfo.html',
            success: function (layero, index) {
                _this = index;
                var frameId = $(layero).find('iframe').attr("id");
                //找到子页面的document对象
                var document = window.frames[frameId].document;
                //根据id找到子页面中相应的元素并赋值recivePersonId
                $(document).find("input[name='recivePersonId']").val('');
            },
            end:function(){
                location.reload();
            }
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
        return false;
    })
   
});

